package com.pan.api.config.impl;

import com.pan.api.config.ConfigUnit;
import com.pan.api.utils.PanApiDocXmlUtil;
import com.pan.api.convert.*;
import com.pan.api.utils.PanReflectUtil;
import org.w3c.dom.Element;

final class PanConvertConfigUnit
    extends ConfigUnit<PanInformationConvert>
    implements PanApiDocXmlUtil, PanReflectUtil
{
    public static PanConvertConfigUnit INSTANCE = new PanConvertConfigUnit();
    /**
     * PanApiDoc Xml form
     * convert:
     *      controller
     *      method
     *      argument
     */
    @Override
    protected PanInformationConvert createNewObjectInstance() {
        Element convertElement = findFirst("convert");
        Element controllerElement = findFirst(convertElement, "controller");
        Element methodElement = findFirst(convertElement, "method");
        Element argumentElement = findFirst(convertElement, "argument");

        String controllerConvertStrategyClassName = controllerElement.getTextContent().strip();
        String methodConvertStrategyClassName = methodElement.getTextContent().strip();
        String argumentConvertStrategyClassName = argumentElement.getTextContent().strip();

        ArgumentStrategy argumentStrategy = createNewInstance(argumentConvertStrategyClassName);
        MethodStrategy methodStrategy = createNewInstance(methodConvertStrategyClassName, argumentStrategy);
        ControllerStrategy controllerStrategy = createNewInstance(controllerConvertStrategyClassName, methodStrategy);
        PanInformationConvert panInformationConvert = new CommonInformationConvert(controllerStrategy);
        return panInformationConvert;
    }

}
